home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
cjdates.exe
/
DATES.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1991-07-07
|
5KB
|
106 lines
unit dates;
{This unit provides date calculation support for Turbo Pascal programs. It
uses the universal date calculation routines in "dates.obj" which have been
designed to work with both Pascal and C, so programs written in either
language will have compatible Day Numbers.
The heart of the scheme is the use of a Day Number which is related to
the "Julian Day Number", the number of days since a date in the distant
past. Note that this is NOT the Julian DATE, which contins a day of the
YEAR. The Day Number used by these routines has two useful properties:
1.) The difference between two Day Numbers is the number of days between
those dates.
2.) The remainder from dividing the Day Number by 7 (DayNumber mod 7) gives
the day of the week, with 0 being Sunday thru 6 being Saturday.
In addition, the conversions will not produce invalid results (unless you
get out of range, in which case zeroes are returned, so you can tell).
That is, any garbage Gregorian date (Month/Day/Year form) will give some
kind of Day Number, and all Day Numbers convert to valid Gregorian dates.
Thus you can check the validity of a Gregorian date by converting it to a
Day Number and back, and then comparing the Gregorian date you get back to
the original. If they are different, the original date was invalid.
For example, if you convert 2/29/1900 to a Day Number you get 694082.
Convert it back and you get 3/1/1900, so 2/29/1900 is not a valid date.
And that's true. Even though it looks like it should be a leap year, the
400-year Gregorian rule says that the leap year is dropped on the century
year unless that century year can be divided by 400 without a remainder.
So if we convert 2/29/2000 to a Day Number we get 730606 which converts back
to 2/29/2000, so that IS a valid date.
Note that the Day Number tends to be what an invalid date "should have been".
That is, in the Feb 29, 1900 case shown above, it gave the Day Number of
March 1, 1900. We can take advantage of this to find the Julian day of
the year. It is simpky the difference between the Day Number of the desired
date and the Day Number of January ZERO (so Jan 1 will come out day 1) of
the same year.
To convert a Julian date to a Gregorian date, Convert to a Day Number the
Month 1, the Day the Julian Day of the Year, and the given year, then
convert back to a Gregorian date. For example, if we have the Julian date
335/1900, we converet 1/335/1900 to a Day Number (694357) which converts
back to 12/1/1900. 335/2000 converts (from 1/335/2000) to 730881, which
converts back to 11/30/2000.
Please note that all values are UNSIGNED! The Day Number will never be so
big as to appear as a negative value. A negative value supplied as a Day
Number by the user will appear to ZDate as a giant (and out of range)
positive number. Year, Month and Day are all WORDs, and are, thus, unsigned.
The caller to ZDate must supply words to hold the returned Gregorian date
components. (You will note that they are VAR parameters.)
When you call ZDay, the Year must not exceed 25599. Dates earlier than
2/1/0 will not convert. The Month and Day may have values in the range
0-65535, but using large values like this will reduce the maximum value
that you can supply for the Year. Normally this will not be a problem.
For dates that cannot be converted, ZDay returns a ZERO value for the Day
Number.
For years before Gregorian dates were used you will not get the correct
values, The conversion to Gregorian dates began in the 14th century, but
were accepted at different times in different countries. No checks are
made for dates before the Gregorian calendar was adopted.
When you call ZDate, the Day Number you supply must be greater than 121
and less than 23920640. If you supply a value outside this range, ZDate
will return a boolean FALSE and your three words (Year, Month and Day)
will be set to zero.
NOTE: In all these routines, the Year is the FULL year, not just the last two
digits!!!!
See also the companion pieces in "dates.inc", which you can either include
as is or read in with ^KR and edit to suit you own needs.}
{(c)Copyright 1991 Crazy Jack}
{All Rights Reserved}
{$D-,O+}
{--------------------------------}INTERFACE{---------------------------------}
function ZDay( {Convert Gregorian date to Day Number. }
Year, {User-supplied year of Gregorian date}
Month, {User-supplied Gregorian month. }
Day : word {User supplied day of month. }
) : longint; {Function returns Day Number. }
function ZDate( {Convert Day Number to Gregorian date: }
DayNumber : longint; {User-supplied Day Number. }
var Year, {User's word receives year. }
Month, {User's holder receives month. }
Day : word {User's holder receives day of month.}
) : boolean; {Function returns FALSE if Day Number|
{is out of range, TRUE otherwise. }
{------------------------------}IMPLEMENTATION{------------------------------}
function ZDay(Year, Month, Day : word) : longint; external;
function ZDate(DayNumber : longint; var Year, Month, Day : word ) : boolean;
external;
{$L dates} {See "dates.asm" for details.}
end.